import { World } from '@ecs/index'
import { getWorldData } from './tool'
import {
  Image,
  Position,
  Render,
  Rotate,
  Score,
  Size,
  Text,
  Velocity
} from '../components'

/**
 * 创建开始按钮
 * @param world 世界实例
 * @returns 无
 */
export function createStart(world: World): void {
  const { canvas, map } = getWorldData(world)
  const image = map.get('start') as HTMLImageElement
  const ascept = image.height / image.width
  const w = image.width / 2
  const h = w * ascept
  world.addEntityWithComponents(
    world.createEntity('start'),
    new Image(image, [{ sx: 0, sy: 0, sw: image.width, sh: image.height }]),
    new Position((canvas.width - w) / 2, (canvas.height - h) / 2),
    new Size(w, h),
    new Render('image', 3)
  )
}

/**
 * 创建计分器
 * @param world 世界实例
 * @returns 无
 */
export function createScore(world: World): void {
  const { canvas } = getWorldData(world)
  world.addEntityWithComponents(
    world.createEntity('score'),
    new Text('得分:0', 1000),
    new Score(0),
    new Position(10, canvas.width / 10),
    new Render('text', 1)
  )
}

/**
 * 创建小鸟
 * @param world 世界实例
 * @returns 无
 */
export function createBird(world: World): void {
  const { canvas, sprites, spritesData } = getWorldData(world)
  const w = spritesData.upBird.sw * 3
  const h = w * (spritesData.upBird.sh / spritesData.upBird.sw)
  world.addEntityWithComponents(
    world.createEntity('brid'),
    new Image(
      sprites,
      [
        {
          sx: spritesData.upBird.sx,
          sy: spritesData.upBird.sy,
          sw: spritesData.upBird.sw,
          sh: spritesData.upBird.sh
        },
        {
          sx: spritesData.middleBird.sx,
          sy: spritesData.middleBird.sy,
          sw: spritesData.middleBird.sw,
          sh: spritesData.middleBird.sh
        },
        {
          sx: spritesData.downBird.sx,
          sy: spritesData.downBird.sy,
          sw: spritesData.downBird.sw,
          sh: spritesData.downBird.sh
        }
      ],
      0.2
    ),
    new Position(canvas.width / 4, canvas.height / 2.5),
    new Size(w, h),
    new Velocity(0, 0, 0.25),
    new Rotate(0, 0, 0.005),
    new Render('image', 2)
  )
}
